package com.ethan.rxretrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.ethan.http.Api;
import com.ethan.http.ApiCall;
import com.ethan.http.ApiException;
import com.ethan.http.ApiFlowableTransformer;
import com.ethan.http.ApiReq;
import com.ethan.http.BaseBean;
import com.ethan.http.RetrofitManager;

import static android.icu.number.Notation.simple;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Api apiClass;

    //进一步封装后回调写法
    private void callBack() {
        ApiReq.req(apiClass.test(), this, new ApiCall<BaseBean<LoginBean>>() {
            @Override
            public void success(BaseBean<LoginBean> t) {
                System.out.println("----------" + t.d.dept);
            }
            @Override
            public void error(Throwable e) {
                ApiException apiException = ApiException.handleException(e);
                Toast.makeText(MainActivity.this, apiException.error, Toast.LENGTH_LONG).show();
            }
        });
    }

    //简单写法
    private void simpleCall() {
        RetrofitManager.getServices(Api.class).test()
                .compose(ApiFlowableTransformer.create(MainActivity.this))
                .subscribe(r -> {
                    System.out.println("----------" + r.d.dept);
                }, e -> {
                    ApiException apiException = ApiException.handleException(e);
                    Toast.makeText(MainActivity.this, apiException.error, Toast.LENGTH_LONG).show();
                });
    }

    //lamada封装写法
    private void lamadaCall() {
        ApiReq.req(apiClass.test(), this, r -> {
            System.out.println("----------" + r.d.dept);
        }, e -> {
            ApiException apiException = ApiException.handleException(e);
            Toast.makeText(MainActivity.this, apiException.error, Toast.LENGTH_LONG).show();
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        apiClass = RetrofitManager.getServices(Api.class);
        findViewById(R.id.bt).setOnClickListener(this);
        findViewById(R.id.bt_1).setOnClickListener(this);
        findViewById(R.id.bt_2).setOnClickListener(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt: {
                simpleCall();
                break;
            }
            case R.id.bt_1: {
                callBack();
                break;
            }
            case R.id.bt_2: {
                lamadaCall();
                break;

            }
        }

    }


}
